package ICM.client;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.SQLException;

import javax.annotation.processing.Messager;
import javax.swing.*;
import javax.swing.border.*;

import common.*;

public class MainWindowUI extends JPanel implements ActionListener{
	
	private ClientUI ui;
	private Client c;
	private JButton btnTreatment;
	private JButton btnMonitoring;
	private JButton btnChange;
	private JButton btnReports;
	private JButton btnExit;
	private JButton btnCommittee;
	private JPanel panel_1;
	private JPanel panel_2;
	private JLabel lblWelcome;


	public MainWindowUI (final Client c, final ClientUI ui)  {
		this.c=c;
		this.ui=ui;
		
		String first_name = (ui.getUser().getFirstname() != null)? ui.getUser().getFirstname() : "";
		String last_name = (ui.getUser().getLastname() != null)? ui.getUser().getLastname() : "";
		String role = ui.getUser().getRole();
		setLayout(new GridBagLayout());

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 2, true));
		panel.setPreferredSize(new Dimension(600,400));
		panel.setSize(new Dimension(600,400));
		panel.setLayout(null);
		
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(5, 0, 5, 0);
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel,gbc_panel);
		
		JLabel lblAesTitle = new JLabel("ICM Main Menu");
		lblAesTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblAesTitle.setVerticalAlignment(SwingConstants.TOP);
		lblAesTitle.setBounds(2, 7, 596, 17);
		lblAesTitle.setFont(new Font("Tahoma", Font.BOLD, 14));
		panel.add(lblAesTitle);
		
		lblWelcome = new JLabel("Welcome " + first_name + " " + last_name + " (" + role + ")");
		lblWelcome.setBounds(200, 52, 400, 14);
		panel.add(lblWelcome);

		panel_1 = new JPanel();
		panel_1.setBounds(2, 84, 596, 100);
		panel.add(panel_1);
		
		btnTreatment = new JButton("Treatment Request" , new ImageIcon(MainWindowUI.class.getResource("/client/images/treatment.png")));
		btnTreatment.addActionListener(this); 
		panel_1.setLayout(null);
		GridBagConstraints gbc_btnTreatment = new GridBagConstraints();
		gbc_btnTreatment.insets = new Insets(5, 5, 5, 5);
		panel_1.add(btnTreatment);
		btnTreatment.setBounds(33, 5, 170, 41);
		
		btnMonitoring = new JButton("Monitoring Request", new ImageIcon(MainWindowUI.class.getResource("/client/images/Monitoring.png")));
		btnMonitoring.addActionListener(this); 
		GridBagConstraints gbc_btnMonitoring = new GridBagConstraints();
		gbc_btnMonitoring.insets = new Insets(5, 5, 5, 5);
		panel_1.add(btnMonitoring);
		btnMonitoring.setBounds(206, 5, 192, 41);
		
		btnChange  = new JButton("Change Request" , new ImageIcon(MainWindowUI.class.getResource("/client/images/change.png")));
		btnChange.addActionListener(this); 
		GridBagConstraints gbc_btnChange = new GridBagConstraints();
		gbc_btnChange.insets = new Insets(5, 5, 0, 5);
		panel_1.add(btnChange);
		btnChange.setBounds(408, 5, 178, 41);
		
		btnReports = new JButton("Reports" , new ImageIcon(MainWindowUI.class.getResource("/client/images/report.png")));
		btnReports.addActionListener(this); 
		GridBagConstraints gbc_btnReports = new GridBagConstraints();
		gbc_btnReports.insets = new Insets(5, 5, 0, 5);
		panel_1.add(btnReports);
		btnReports.setBounds(160, 59, 125, 41);
		
		btnCommittee = new JButton("Committee" ,new ImageIcon(MainWindowUI.class.getResource("/client/images/group.png")));
		btnCommittee.addActionListener(this);
		btnCommittee.setVisible(false);
		btnCommittee.setBounds(330, 64, 140, 36);
		panel_1.add(btnCommittee);
		
		panel_2 = new JPanel();
		panel_2.setBounds(2, 347, 596, 51);
		panel.add(panel_2);
		panel_2.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		
		btnChange.setVisible(false);
		btnMonitoring.setVisible(false);
		btnTreatment.setVisible(false);
		btnReports.setVisible(false);
		
	
		btnExit = new JButton ("Logout"  , new ImageIcon(MainWindowUI.class.getResource("/client/images/logout.png")));
		btnExit.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					CommandMessage msg = new CommandMessage(CommandMessage.LOGOUT_USER,new Integer[] {ui.getUser().getId()});
					try {
						c.sendToServer(msg);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
					try {
					    Thread.sleep(200);
					} catch(InterruptedException ex) {
					    Thread.currentThread().interrupt();
					}
					ui.SetContent(ClientUI.LOGIN_SCREEN);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			
		}); 
		GridBagConstraints gbc_btnExit = new GridBagConstraints();
		gbc_btnExit.insets = new Insets(5, 5, 0, 5);
		panel_2.add(btnExit, gbc_btnExit);
		btnExit.setBounds(353, 7, 57, 23);
		
		
		// Set welcome message.
		if (ui.getUser() != null) 
		{
		
			btnChange.setVisible(true);
			btnMonitoring.setVisible(true);
			if (ui.getUser().getRole().equals("System Engineer") || (ui.getUser().getRole().equals("Manager")) || (ui.getUser().getRole().equals("Committee member")) || (ui.getUser().getRole().equals("Head of committee")))
				btnTreatment.setVisible(true);
			
			if (ui.getUser().getRole().equals("Manager") )
			{
				btnCommittee.setVisible(true);
				btnReports.setVisible(true);
			}
				
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource().equals(btnTreatment)) {
			try {
				if (ui.getUser().getRole().equals("Manager"))
					ui.SetContent(ui.Treatment_Screen);
				else if (ui.getUser().getRole().equals("System Engineer") || ui.getUser().getRole().equals("Committee member") || ui.getUser().getRole().equals("Head of committee"))
					ui.SetContent(ui.SystemEngineerWindow_Screen);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		else if (e.getSource().equals(btnMonitoring)) {			
			try {
				ui.SetContent(ui.Monitoring_Screen);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
		}
		else if (e.getSource().equals(btnChange)){
			try {
				ui.SetContent(ui.ChangeRequest_Screen);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		else if (e.getSource().equals(btnReports)) {
				try {
					ui.SetContent(ui.Report_Screen);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		
		}
		else if (e.getSource().equals(btnCommittee)) {
			try {
				ui.SetContent(ui.Committee_Screen);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	
	}
	}
}
